package machine;

import java.io.PrintStream;

class RegisterBank
{

    String registers[];
    public static final int numRegisters = 4;
    
    public RegisterBank()
    {
        registers = new String[numRegisters];
        for(int i = 0; i <= numRegisters-1; i++)
        {
            registers[i] = "00000000000000000000000000000000";
        }

    }

    public String read(String s)
    {
        try
        {
            int i = Integer.parseInt(s, 2);
            if(i >= 0 && i < numRegisters)
            {
                return registers[i];
            } else
            {
                System.out.println("Attempt to read to nonexistent register #" + i);
                return null;
            }
        }
        catch(NumberFormatException numberformatexception)
        {
            System.out.println("Error reading register: " + s + ".");
        }
        return null;
    }

    public void write(String s, String s1)
    {
        try
        {
            int i = Integer.parseInt(s, 2);
            if(i >= numRegisters)
            {
                System.out.println("Attempt to write to nonexistent register.");
            } else
            {
                registers[i] = s1;
            }
        }
        catch(NumberFormatException numberformatexception)
        {
            System.out.println("Error writing register: " + s + ".");
        }
    }

    public void reset()
    {
        for(int i = 0; i < numRegisters; i++)
        {
            registers[i] = "00000000000000000000000000000000";
        }

    }

    public static void main(String args[])
    {
        RegisterBank registerbank = new RegisterBank();
        for(int i = 1; i < numRegisters; i++)
        {
            System.out.println(i + ": " + registerbank.read(Integer.toBinaryString(i)));
        }

        for(int j = 1; j < numRegisters; j++)
        {
            String s;
            for(s = Integer.toBinaryString(j); s.length() < 32; s = "0" + s) { }
            registerbank.write(Integer.toBinaryString(j), s);
        }

        for(int k = 1; k < numRegisters; k++)
        {
            System.out.println(k + ": " + registerbank.read(Integer.toBinaryString(k)));
        }

    }

}
